import { forIn } from 'lodash';
import { ref, watch, unref, computed, onMounted, nextTick, toRaw, inject } from 'vue';
import fieldWidget from './field-widget'
import containerWidget from './container-widget'


export function useWidget() {
  // const componentProps = ref<any>({})

  const watchDefaultValue = (props) => {
    const widget = props.widget
      // 针对表单字段的默认值
    watch(
      () => widget.props.defaultValue,
      (val: any) => {
        widget.props.value = val
      },
      {
        deep: true,
        immediate: true
      }
    );
  }

  const hasWidget = (name) => {
    const exist = containerWidget[`${name}-widget`] || fieldWidget[`${name}-widget`]
    if (!exist) {
      const tip = `不存在 ${name}-widget 组件, 请核对 widgetConfig.js 中的 type 是否正确`
      console.warn(tip)
    }
    return exist
  }

  return {
    watchDefaultValue,
    hasWidget
  }
}